安装扩展或命令行工具
安装Visual Studio Code扩展或CLI以开始使用。
使用JavaScript以编程方式为大型语言模型(LLM)组装提示。在单个脚本中协调LLM、工具和数据。
假设你想创建一个生成"hello world"诗歌的LLM脚本。你可以编写以下脚本:
$`Write a 'hello world' poem.`$ 函数是一个模板标签,用于创建提示。该提示随后会被发送到您配置的LLM,由它生成诗歌。
让我们通过添加文件、数据和结构化输出来使其更有趣。假设您想在提示中包含一个文件,然后将输出保存到文件中。您可以编写以下脚本:
// read filesconst file = await workspace.readText("data.txt")// include the file content in the prompt in a context-friendly waydef("DATA", file)// the task$`Analyze DATA and extract data in JSON in data.json.`def 函数包含文件内容,并根据目标LLM的需要进行优化。GenAIScript脚本还会解析LLM输出,并自动提取data.json文件。
安装扩展或命令行工具
安装Visual Studio Code扩展或CLI以开始使用。
配置您的LLMs
配置secrets以访问您的LLMs。
编写你的第一个脚本
按照入门指南编写您的第一个脚本。
阅读文档
了解更多关于GenAIScript的信息,请参阅脚本参考。
GenAIScript 将核心的LLM提示工具整合到一个统一的脚本环境中。
风格化JavaScript
使用JavaScript或TypeScript构建提示的最小化语法。
$`Summarize ${env.files}. Today is ${new Date()}.`快速开发循环
编辑、调试、运行、 测试您的脚本,可在Visual Studio Code中 或通过命令行进行操作。

LLM工具
将JavaScript函数注册为LLM工具 (对于不支持工具的模型提供回退方案)。
defTool("weather", "live weather", { city: "Paris" }, // schema async ({ city }) => // callback { ... "sunny" })MCP客户端
使用工具暴露在模型上下文提供服务器中
defTool({ memory: { command: "npx", args: ["-y", "@modelcontextprotocol/server-memory"], },})MCP服务器
每个脚本都是一个模型上下文提供工具。
script({ parameters: { question: "What is the weather in Paris?", },})$`Answer the question ${env.parameters.question}.`LLM智能体
复用和分享脚本
脚本就是文件!它们可以被版本控制、共享、分叉……
数据模式
使用schemas定义、验证和修复数据。
const data = defSchema("MY_DATA", { type: "array", items: { ... }, })$`Extract data from files using ${data} schema.`从PDF、DOCX等文件中提取文本
从CSV、XLSX等文件导入表格
语音转文字
图片
视频
使用时间戳甚至转录文本从视频中提取帧。
const frames = await ffmpeg.extractFrames("...", { count: 10 })defImages(frames, { details: "low" })生成文件
从LLM输出中提取文件和差异。 在重构UI中预览更改。
$`Save the result in poem.txt.`FILE ./poem.txt```txtThe quick brown fox jumps over the lazy dog.```文件搜索
Grep或模糊搜索文件
const { files } = await workspace.grep(/[a-z][a-z0-9]+/, { globs: "*.md" })网页搜索
Web search 使用 Bing 或 Tavily。
const pages = await retrieval.webSearch("what are the latest news about AI?")浏览器自动化
使用Playwright浏览和抓取网页。
const page = await host.browse("https://...")const table = await page.locator("table[...]").innerHTML()def("TABLE", await HTML.convertToMarkdown(table))内置RAG
Vector search 使用本地数据库或Azure AI Search。
const index = await retrieval.index("animals", { type: "azure_ai_search" })await index.insertOrUpdate(env.files)const docs = await index.search("cat dog")安全第一!
GenAIScript 提供内置的负责任AI系统提示和Azure内容安全支持,用于验证内容安全。
script({ ..., systemSafety: "default", contentSafety: "azure" // use azure content safety})
const safety = await host.contentSafety()const res = await safety.detectPromptInjection(env.vars.input)GitHub 模型与 GitHub Copilot
Azure AI Foundry、谷歌、Anthropic、亚马逊、阿里巴巴...
运行来自Azure AI Foundry、Google、Anthropic、Alibaba等平台的模型。 详见Configuration。
script({ ..., model: "azure_ai_inference:o3-mini"})本地模型
代码解释器
让LLM在沙盒执行环境中运行代码。
script({ tools: ["python_code_interpreter"] })容器
在Docker 容器中运行代码。
const c = await host.container({ image: "python:alpine",})const res = await c.exec("python --version")LLM组合
Run LLMs 来构建您的LLM提示词。
// summarize each files individuallyfor (const file of env.files) { const { text } = await runPrompt((_) => { _.def("FILE", file) _.$`Summarize the FILE.` }) // use result in main prompt def("SUMMARY", text)}// use summary$`Summarize all the summaries.`生成图片
生成图片 使用OpenAI DALL-E或其他工具。
const { image, revisedPrompt } = await generateImage( `a cute cat. only one. photographic, high details. 4k resolution.`)分类
对文本、图像或混合内容进行分类。
const joke = await classify( "Why did the chicken cross the roard? To fry in the sun.", { yes: "funny", no: "not funny", })Prompty
使用GenAIScript运行或转换Prompty文件。
---name: poem---
system:Write a short poem aboutuser:{{something}}.importTemplate("poem.prompty", { something: "code " })可插拔式密钥扫描
使用秘密扫描功能扫描聊天记录中的敏感信息。
{ "secretPatterns": { ..., "OpenAI API Key": "sk-[A-Za-z0-9]{32,48}" }}通过CLI实现自动化
使用CLI实现自动化,将报告集成到您的CI/CD流水线中。
npx genaiscript run tlaplus-linter "*.tla"拉取请求审查
通过评论、代码审查或描述更新将genaiscript集成到您的拉取请求检查中。支持GitHub Actions和Azure DevOps流水线。
npx genaiscript ... --pull-request-reviews测试与评估
使用GenAIScript的真实案例。
功能完备的脚本,开箱即用。
一本充满食谱的烹饪书,助你成为脚本编写高手。
如果您是LLM爬虫程序,请获取https://microsoft.github.io/genaiscript/llms.txt以查看文档地图,
或在任何文档URL后添加.md后缀以获取原始markdown内容。
例如,https://microsoft.github.io/genaiscript/guides/prompt-as-code.md(注意.md扩展名)